using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Net;
using TMM.Common;
using Android.Util;

namespace TMM
{
    public class BaseActivity : Activity
    {
        public Actions CurrentAction { get; internal set; }
        public Android.Net.Uri UpdateUrl { get; internal set; }

        public virtual void Initialize() { }
        public virtual void OnNoInternetAccess() { }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Initialize();
        }

        public bool HasAccessToInternet()
        {
            try
            {
                var connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);

                if (connectivityManager == null)
                    return false;
                var activeConnection = connectivityManager.ActiveNetworkInfo;
                if (activeConnection == null || !activeConnection.IsConnected)
                    return false;
                activeConnection.Dispose();
                return true;
            }
            catch (Exception)
            {

            }

            return false;
        }

        public void ShowUpdateApplicationPopup(string url, IDialogInterfaceOnClickListener listener)
        {
            if (string.IsNullOrEmpty(url))
                return;

            CurrentAction = Actions.UpdatingApplication;

            UpdateUrl = Android.Net.Uri.Parse(url);

            var alert = new AlertDialog.Builder(this);
            alert.SetTitle("Actualizare aplicatie");
            alert.SetMessage("Este necesara actualizarea aplicatiei la o versiune mai noua.");
            alert.SetPositiveButton("Actualizare", listener);
            alert.SetNegativeButton("Iesire", listener);

            alert.Show();
        }
    }
}